package com.javaee.ejb.session;

import java.util.List;

import com.javaee.ejb.entity.Conversation;
import com.javaee.ejb.entity.Employee;
import com.javaee.ejb.session.remote.ConversationManagerRemote;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class ConversationManager
 */
@Stateless
public class ConversationManager implements ConversationManagerRemote {

	@PersistenceContext
	private EntityManager entityManager;

	public ConversationManager() {
	}

	public int saveConversation(Conversation conversation) {
		conversation = entityManager.merge(conversation);
		entityManager.persist(conversation);
		return conversation.getId();
	}

	@SuppressWarnings("unchecked")
	public List<Conversation> getListByEmployee(int employeeId) {
		Query query = entityManager
				.createQuery("SELECT DISTINCT Conversation FROM Conversation conversation INNER JOIN conversation.called called INNER JOIN conversation.caller caller WHERE caller.id = :employeeId OR called.id = :employeeId ORDER BY start DESC");
		query.setParameter("employeeId", employeeId);

		return (List<Conversation>) query.getResultList();
	}

	@SuppressWarnings("unchecked")
	public List<Conversation> getListByProjectId(int projectId) {
		Query query = entityManager
				.createQuery("SELECT DISTINCT conversation FROM Conversation conversation INNER JOIN conversation.project project WHERE project.id = :projectId");
		query.setParameter("projectId", projectId);
		return (List<Conversation>) query.getResultList();
	}

	public Conversation getByIdWithDependencies(int id) {
		Query query = entityManager
				.createQuery("SELECT DISTINCT conversation FROM Conversation conversation INNER JOIN FETCH conversation.called INNER JOIN FETCH conversation.caller INNER JOIN FETCH conversation.project WHERE conversation.id = :id");
		query.setParameter("id", id);

		try {
			return (Conversation) query.getSingleResult();
		} catch (Exception e) {
			return null;
		}
	}

	public Conversation getById(int id) {
		return entityManager.find(Conversation.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Employee> getInterlocutorListByProject(int employeeId,
			int projectId) {
		List<Employee> interlocutorList = null;

		Query query = entityManager
				.createQuery("SELECT DISTINCT called FROM Conversation conversation INNER JOIN conversation.called called INNER JOIN conversation.caller caller INNER JOIN conversation.project project WHERE caller.id = :employeeId AND project.id = :projectId AND conversation.status < 2");
		query.setParameter("employeeId", employeeId);
		query.setParameter("projectId", projectId);

		interlocutorList = (List<Employee>) query.getResultList();

		query = entityManager
				.createQuery("SELECT DISTINCT caller FROM Conversation conversation INNER JOIN conversation.called called INNER JOIN conversation.caller caller INNER JOIN conversation.project project WHERE called.id = :employeeId AND project.id = :projectId AND conversation.status < 2");
		query.setParameter("employeeId", employeeId);
		query.setParameter("projectId", projectId);

		interlocutorList.addAll(query.getResultList());

		return interlocutorList;
	}
}
